
import { GlobalManager } from "../global/GlobalManager";
import { GlobalRoot } from "../global/GlobalRoot";
export enum SeverUrl {
    /**获取存档 */
    getServerInfo = "/getServerInfo",
    saveServerInfo = "/saveServerInfo",
}



export default class NetworkManager {
    /**这些接口需要刷新信息 */
   
    private static TimeOut: number = 3000
    /**获取今日日期 */
    protected static getDay(): string {
        let str: string = "";
        let today = new Date();
        str = today.getFullYear() + "" + (today.getMonth() + 1) + "" + today.getDate();
        return str;
    }




    /**获取信息 */
    public static postServerInfo(data: any, callback: Function, type: SeverUrl, isErrorCode: boolean = false) {
        this.requireTimes++
        let _URLPath: string = GlobalManager.httpUrl + type;
        let _data = data
        this.POST(_URLPath, _data, (a, b) => {
            let bb = null
            if (a && b) {
                bb = JSON.parse(b);
            }

            if (a && bb && (bb.code == 200) || (isErrorCode)) {
                this.requireTimes = 0

                GlobalRoot.instance.UiManager.hideBlockLayer()
                callback(true, bb);


            } else {
                GlobalRoot.instance.UiManager.hideBlockLayer()
                callback(false, "httpLost");
                if (bb && bb.msg) {
                    GlobalRoot.instance.UiManager.showTips(data.msg)


                }

            }
        })
    }


    public static requireTimes = 0
    /**获取信息 */
    public static getServerInfo(data: any, callback: Function, type: SeverUrl, isErrorCode: boolean = false) {
        let _URLPath: string = GlobalManager.httpUrl + type;
        let _data = data
        this.requireTimes++
        this.GET(_URLPath, _data, (a, b) => {
            let bb = null
            if (a && b) {
                bb = JSON.parse(b);
            }

            if (a && bb && (bb.code == 200 || isErrorCode)) {
                let bb = JSON.parse(b);
                GlobalRoot.instance.UiManager.hideBlockLayer()
                callback(true, bb);


            } else {
                GlobalRoot.instance.UiManager.hideBlockLayer()
                if (bb && bb.msg) {
                    GlobalRoot.instance.UiManager.showTips(data.msg)

                }
                callback(false, "httpLost");

            }
        })
    }



    /**
     * POST请求
     *
     * @static
     * @param {*} url
     * @param {object} [param={}]
     * @param {*} callback
     * @memberof HttpUtil
     */
    public static async POST(url, param: object = {}, callback) {
        let signedParams = { ...param };

        var xhr = new XMLHttpRequest();
        const dataStr = JSON.stringify(signedParams);
        xhr.open("POST", url, true);
        xhr.setRequestHeader("Content-Type", "application/json");
        let key = GlobalManager.shortToken
        if (key) {
            xhr.setRequestHeader("Authorization", key);
        }
        xhr.onreadystatechange = function () {
            if (xhr.readyState === 4) {
                let response = xhr.responseText;
                if (xhr.status >= 200 && xhr.status < 500) {
                    let httpStatus = xhr.statusText;
                    callback(true, response);
                } else {
                    callback(false, xhr);
                }
            }
        };
        xhr.send(dataStr);
    }


    /**
     * GET请求
     *
     * @static
     * @param {*} url
     * @param {object} [params={}]
     * @param {*} callback
     * @memberof HttpUtil
     */
    public static async GET(url, params: object = {}, callback) {
        let signedParams = { ...params };

        // 构建查询字符串
        let dataStr = '';
        Object.keys(signedParams).forEach(key => {
            dataStr += key + '=' + encodeURIComponent(signedParams[key]) + '&';
        });
        if (dataStr !== '') {
            dataStr = dataStr.substring(0, dataStr.lastIndexOf('&'));
            url = url + '?' + dataStr;
        }


        let xhr = new XMLHttpRequest();
        xhr.open("GET", url, true);
        xhr.setRequestHeader("Content-Type", "text/plain;charset=UTF-8");
        let key = GlobalManager.shortToken
        if (key) {
            xhr.setRequestHeader("Authorization", key);
        }

        xhr.onreadystatechange = function () {
            if (xhr.readyState === 4) {
                let response = xhr.responseText;
                if (xhr.status >= 200 && xhr.status < 500) {
                    let httpStatus = xhr.statusText;
                    callback(true, response);
                } else {
                    callback(false, xhr);
                }
            }
        };
        xhr.timeout = this.TimeOut;
        xhr.send();
    }


}